#include "driver_radar.h"
#include "tick.h"
#include "main.h"

void radarInit(Radar radars[], uint8_t size)
{
    GPIO_InitTypeDef GPIO_Key_InitConfig;
    RADAR1_GPIO_CLK_ENABLE();
    GPIO_Key_InitConfig.Mode = GPIO_MODE_INPUT;
    GPIO_Key_InitConfig.Pull = GPIO_PULLUP;
    GPIO_Key_InitConfig.Speed = GPIO_SPEED_FREQ_HIGH;
    for (int i = 0; i < size; i++)
    {
        GPIO_Key_InitConfig.Pin = radars[i].pin;
        HAL_GPIO_Init(radars[i].GPIOx, &GPIO_Key_InitConfig);
    }
}

GPIO_PinState is_pressed = GPIO_PIN_RESET;
// 按键扫描函数
void radarScan(Radar *radars, uint8_t size)
{
    if (millis() < 2000)
    {
        // 雷达还没有初始化好
        for (int i = 0; i < size; i++)
        {

            radars[i].state = RADAR_IDEL;
        }
        return;
    }
    
    for (int i = 0; i < size; i++)
    {
        // is_pressed = radars[i].readIO(radars[i].GPIOx, radars[i].pin);
				is_pressed = HAL_GPIO_ReadPin(radars[i].GPIOx, radars[i].pin);
        radars[i].state = is_pressed == GPIO_PIN_RESET ? RADAR_OFF : RADAR_ON;
        radars[i].singalStartTime = millis();
    }
}

bool readRadarPin(GPIO_TypeDef *GPIOx, uint8_t pin)
{
    return HAL_GPIO_ReadPin(GPIOx, pin) == KEY_DOWN;
}
